package com.mxchip.livestar.ui.wificamera;

import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.TextureView.SurfaceTextureListener;
import android.view.View;
import android.view.ViewGroup;

import com.mxchip.livestar.R;
import com.mxchip.livestar.av.camera.EncoderCallback;
import com.mxchip.livestar.av.camera.WifiCamera;

import org.jetbrains.annotations.NotNull;

import androidx.fragment.app.Fragment;

public class WifiCameraVideoFragment extends Fragment {

	public static final String TAG = "WifiCameraVideoFragment";
	
	private static  int mVideoId;

	private TextureView mVideoTextureView;
	private boolean mAlreadyStarted;
	
	
	public static WifiCameraVideoFragment newInstance(int  video_id) {
		WifiCameraVideoFragment fragment = new WifiCameraVideoFragment();
		Bundle args = new Bundle();
		mVideoId = video_id;
		
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		Log.d(TAG, "onCreateView");
		
		View view = inflater.inflate(R.layout.fragment_wificamera_video, container, false);
	
        mVideoTextureView = (TextureView)view.findViewById(R.id.fragment_videoview_surface);
        mVideoTextureView.setSurfaceTextureListener(new TextureCallback());
		 
		return view;
	}
	


	@Override
	public void onResume(){
		Log.d(TAG, "onResume");
		super.onResume();
		
	}
	
    @Override
    public void onPause() {
    	Log.d(TAG, "onPause");
        super.onPause();
        
    }

    @Override
    public void onDestroy() {
    	Log.d(TAG, "onDestroy");
        super.onDestroy();
        
    }

    private EncoderCallback callback = new EncoderCallback() {
		@Override
		public void onH264FramePacket(@NotNull byte[] data, @NotNull String deviceName, int width, int height, int rotation, long timestamp) {
			Log.d(TAG, "onH264FramePacket " + data + ", " + deviceName + ", " + width + ", " + height);
		}
	};

	private final class TextureCallback implements SurfaceTextureListener {

		@Override
		public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
//		    FIleManager.create();
			// mNativeWifiCamera = WifiCamera.getInstance();
            mAlreadyStarted = true;
            Log.d(TAG, "onSurfaceTextureAvailable(), width = " + width + ", height = " + height+" video_id="+mVideoId);
            if (mAlreadyStarted) {
				// WifiCamera.getInstance().open(mVideoId);
				 WifiCamera.getInstance().setDecodeTexture(mVideoTextureView.getSurfaceTexture());
				// WifiCamera.getInstance().configure(width, height);
				WifiCamera.getInstance().setEncoderCallback(callback);
				WifiCamera.getInstance().start();
                mAlreadyStarted = false;
            }
		}

		@Override
		public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
			 Log.d(TAG, "onSurfaceTextureSizeChanged(), width = " + width + ", height = " + height+" video_id="+mVideoId);
		}

		@Override
		public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
			Log.d(TAG, "onSurfaceTextureDestroyed(), video_id="+mVideoId);
			WifiCamera.getInstance().stop();
            mAlreadyStarted = true;
//			FIleManager.close();
			return false;
		}

		@Override
		public void onSurfaceTextureUpdated(SurfaceTexture surface) {
			//LogUtils.d(TAG, "onSurfaceTextureUpdated(), video_id="+mVideoId);	
		}
		
	}
}
